import {Component} from '@angular/core';
import {
    AlertController, IonicPage, LoadingController, NavController, NavParams, Platform,
    ToastController
} from 'ionic-angular';
import {SystemService} from "../../service/SystemService";
import {BasePage} from "../../BasePage";
import {Log} from "../../../framework/utils/Log";
import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer';
import { File } from '@ionic-native/file';
import {FileOpener} from "@ionic-native/file-opener";
import {DomSanitizer} from "@angular/platform-browser";
/**
 * Generated class for the NotifyItemPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@Component({
    selector: 'page-notify-item',
    templateUrl: 'notify-item.html',
    providers: [SystemService]
})
export class NotifyItemPage extends BasePage {
    private static TAG: string = 'NotifyItemPage';
    public context: any = {
        BiaoTi: '',
        FaBuSJ: '',
        NeiRong: ''
    };
    notifyId: string;
    constructor(public navCtrl: NavController, public navParams: NavParams, public sys: SystemService, public platform: Platform,
                public loadingCtrl: LoadingController,
                public toastCtrl: ToastController,
                public alertCtrl: AlertController,
		        public sanitizer:DomSanitizer,
                public fileTransfer:FileTransferObject,
                private fileOpener: FileOpener,
                public file:File) {
        super(platform, loadingCtrl, toastCtrl, alertCtrl);
        this.notifyId = this.navParams.data.id;

    }

    ionViewDidLoad() {
        Log.w(NotifyItemPage.TAG,'ionViewDidLoad NotifyItemPage');
        this.getNotifyItem();
    }

    getNotifyItem() {
        this.showLoading(true,'请稍后');
        let data: any = {
            BianHao: this.notifyId
        };
        this.sys.getNotifyDetail(data).subscribe((res: any) => {
            if (res.msgCode == '1') {
                Log.printObj(NotifyItemPage.TAG, 'printObj==', res.detail);
                this.context = res.detail;
            }else {
                this.showError('数据请求失败');
            }
            this.showLoading(false,'请稍后');
        }, error => {
            this.showLoading(false,'请稍后');
            this.showError('数据请求失败');

        })
    }
    downFile(item){
        this.fileTransfer.download(item.FuJianLJ, this.file.dataDirectory + item.FuJianMC).then((entry) => {
            Log.w(NotifyItemPage.TAG,'download complete: ' + entry.toURL());
            this.fileTransfer.onProgress(progressEvent => {
                if (progressEvent.lengthComputable) {
                    Log.w('file progress loading...',progressEvent.loaded / progressEvent.total);
                    let num:any =progressEvent.loaded / progressEvent.total;
                    if(num==1){
                        this.openFile(entry.nativeURL)
                    }
                } else {
                    this.showError('下载失败')
                }
            });
        }, (error) => {
            // handle error
            this.showError('下载失败');
        });
    }

    /**
     * 打开文件
     * @param path
     */
    openFile(path){
        this.fileOpener.open(path, this.getFileMimeType(path.substring(path.lastIndexOf(".")+1,path.length)))
            .then(() => console.log('File is opened'))
            .catch(e => this.showError('打开失败'));
    }

    getFileMimeType(fileType: string) {
        let mimeType: string = '';

        switch (fileType) {
            case 'txt':
                mimeType = 'text/plain';
                break;
            case 'docx':
                mimeType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
                break;
            case 'doc':
                mimeType = 'application/msword';
                break;
            case 'pptx':
                mimeType = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
                break;
            case 'ppt':
                mimeType = 'application/vnd.ms-powerpoint';
                break;
            case 'xlsx':
                mimeType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
                break;
            case 'xls':
                mimeType = 'application/vnd.ms-excel';
                break;
            case 'zip':
                mimeType = 'application/x-zip-compressed';
                break;
            case 'rar':
                mimeType = 'application/octet-stream';
                break;
            case 'pdf':
                mimeType = 'application/pdf';
                break;
            case 'jpg':
                mimeType = 'image/jpeg';
                break;
            case 'png':
                mimeType = 'image/png';
                break;
            default:
                mimeType = 'application/' + fileType;
                break;
        }
        return mimeType;
    }
    assembleHTML(strHTML:any){
        return this.sanitizer.bypassSecurityTrustHtml(strHTML);
    }
}
